package restless.core.gameObjs {
	import flash.display.BitmapData;
	import flash.filters.BitmapFilter;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import restless.core.interfaces.I2d;
	import flash.text.TextFieldAutoSize;
	
	/**
	 * @author MNS
	 */
	
	public class RLETextObj extends RLEManagedObj {
		
		private var tfOver:TextFormat;
		private var tfOut:TextFormat;
		private var _text:String = "'De fault: The two best words in the english language.' - Homer Simpson";
		private var field:TextField;
		private var bChange:Boolean = true;
		private var _master:I2d;
		private var bFollow:Boolean = false;
		private var xMasterOff:Number = 0;
		private var yMasterOff:Number = 0;
		private var bBackground:Boolean = false;
		private var bCenterOnMaster:Boolean = false;
		
		public function RLETextObj() {
			setRE();
			field = new TextField();
			tfOut = textM.getTextFormat(textM.DEFAULT_FORMAT);
			visible = true;
			//rendering = false;
		}
		
		public function setOverFormat(tf:TextFormat):void {
			tfOver = tf;
		}
		
		public function setOutFormat(tf:TextFormat):void {
			tfOut = tf;
		}
		
		public function set text(t:String):void {
			if (sID == "restart") {
				trace ("width " + width + "|| height " + height);
			}
			
			_text = t;
			field.text = _text;
			field.setTextFormat(tfOut);
			field.autoSize = TextFieldAutoSize.LEFT;
			field.cacheAsBitmap = true;
			field.embedFonts = true;
			field.backgroundColor = 0xFFAAFF;
			width = field.width;
			height = field.height;
			
			setRenderBoundary(width, height);
			updateCenterXY();
			bChange = true;
		}
		
		public function get text():String {
			return _text;
		}
		
		override public function update():void {
			if (bFollow) {
				x = _master.x;
				y = _master.y;
				
				if (bCenterOnMaster) {
					x -= _master.centerX;
					y -= _master.centerY;
				}
				
				if (center) {
					x -= centerX;
					y -= centerY;
				}
				
				x += xMasterOff;
				y += yMasterOff;
			}
			
			screenM.onCamera(this);
		}
		
		
		public function background(bool:Boolean, color:uint):void {
			if (bool == bBackground) { return; }
			bBackground = bool;
			if (bBackground) {
				field.background = true;
				field.backgroundColor = color;
			} else {
				field.background = false;
			}
		}
		
		override public function set x(num:Number):void {
			field.x = rect.x = num;
		}
		
		override public function set y(num:Number):void {
			field.y = rect.y = num;
		}
		
		override public function forceRender():void {
			
			super.bitmapData = new BitmapData( width + 1, height + 1, true, 0x00000000 );
			super.bitmapData.draw( field, null, null, null, null, true );
			
			if (bFilter) {
				for (i = 0; i <= _filter.length - 1; i++) {
					super.bitmapData.applyFilter( super.bitmapData, super.bitmapData.rect, tempPoint, _filter[i] );
					width = super.bitmapData.width;
					height = super.bitmapData.height;
				}
			}
			
			width = super.bitmapData.width;
			height = super.bitmapData.height;
			if (sID == "restart") {
				trace ("width " + width + "|| height " + height);
			}
			//setRenderBoundary(width, height);
			updateCenterXY();
		} 
		
		override public function get bitmapData():BitmapData {			
			if (bChange) {
				forceRender();
				bChange = false;
			}
			return super.bitmapData;
		}
		
		override public function addfilter(filter:BitmapFilter):void {
			super.addfilter(filter);
			bFilter = true;
			bChange = true;
		}
		
		override public function mOver():void {
			field.setTextFormat(tfOver);
			bChange = true;
		}
		
		override public function mOut():void {
			field.setTextFormat(tfOut);
			bChange = true;
		}
		
		public function set master(obj:I2d):void {
			_master = obj;
		}
		
		public function get master():I2d {
			return _master;
		}
		
		public function shouldItFollow(bool:Boolean, centerOnMaster:Boolean = false, xOff:int = 0, yOff:int = 0 ):void {
			if (bFollow == bool) { return; }
			bCenterOnMaster = center;
			bFollow = bool;
			
			xMasterOff = xOff;
			yMasterOff = yOff;
		}
		
		override public function dispose():void {
			tfOver = null;
			tfOut = null;
			field = null;
			_master = null;
			super.dispose();
		}
	}
}